#include <plugin/util/SecurityManager.h>

namespace antu
{
namespace plugin
{
SecurityManager::SecurityManager()
    : m_pPrincipal(nullptr),
      m_pSecurityHandler(nullptr)
{
}

SecurityManager::~SecurityManager()
{
    if (this->m_pPrincipal != nullptr)
        delete this->m_pPrincipal;

    if (this->m_pSecurityHandler != nullptr)
        delete this->m_pSecurityHandler;
}

AbstractPrincipal *SecurityManager::currentPrincipal()
{
    return this->m_pPrincipal;
}

void SecurityManager::login(const QString &credential, const QString &token)
{
    if (this->m_pPrincipal != nullptr)
        delete this->m_pPrincipal;

    this->m_pPrincipal = this->authenticate(credential, token);
}

bool SecurityManager::isAllowed(const QString &moduleCode) const
{
    if (this->m_pSecurityHandler != nullptr)
        return this->m_pSecurityHandler->isAllowed(moduleCode);
    else
        return false;
}

bool SecurityManager::isAllowed(const QString &moduleCode, const QString &unit) const
{
    if (this->m_pSecurityHandler != nullptr)
        return this->m_pSecurityHandler->isAllowed(moduleCode, unit);
    return false;
}

void SecurityManager::setSecurityHandler(AbstractSecurityContextHandler *pHandler)
{
    Q_ASSERT(pHandler != nullptr);

    if (this->m_pSecurityHandler != nullptr)
        delete this->m_pSecurityHandler;

    this->m_pSecurityHandler = pHandler;
}

}
}

